package org.goodsmanager.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.goodsmanager.entity.Product;
import org.goodsmanager.service.ProductService;
import org.goodsmanager.util.ExcelExportUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 数据导出Controller
 *
 * @author Goods Manager System
 * @version 1.0.0
 */
@Slf4j
@Controller
@RequestMapping("/export")
@RequiredArgsConstructor
public class ExportController {

    private final ProductService productService;

    /**
     * 导出所有商品到Excel
     */
    @GetMapping("/products/all")
    public void exportAllProducts(HttpServletResponse response) {
        try {
            log.info("开始导出所有商品数据");
            List<Product> products = productService.getAllProducts();
            ExcelExportUtil.exportProductsToExcel(products, response);
            log.info("导出所有商品数据成功，共{}条", products.size());
        } catch (Exception e) {
            log.error("导出商品数据失败", e);
            throw new RuntimeException("导出失败: " + e.getMessage());
        }
    }

    /**
     * 按条件导出商品到Excel
     */
    @GetMapping("/products/filtered")
    public void exportFilteredProducts(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String categoryId,
            @RequestParam(required = false) Integer status,
            HttpServletResponse response) {
        try {
            log.info("开始导出筛选商品数据，关键字: {}, 分类: {}, 状态: {}",
                    keyword, categoryId, status);

            // 查询所有符合条件的数据（不分页）
            var pageData = productService.getProductsByPage(
                    1, Integer.MAX_VALUE, keyword, categoryId, status, null, null, "created_at", "DESC");

            @SuppressWarnings("unchecked")
            List<Product> products = (List<Product>) pageData.get("products");

            ExcelExportUtil.exportProductsToExcel(products, response);
            log.info("导出筛选商品数据成功，共{}条", products.size());
        } catch (Exception e) {
            log.error("导出商品数据失败", e);
            throw new RuntimeException("导出失败: " + e.getMessage());
        }
    }

    /**
     * 导出库存预警商品
     */
    @GetMapping("/products/lowStock")
    public void exportLowStockProducts(HttpServletResponse response) {
        try {
            log.info("开始导出库存预警商品");
            List<Product> products = productService.getLowStockProducts();
            ExcelExportUtil.exportProductsToExcel(products, response);
            log.info("导出库存预警商品成功，共{}条", products.size());
        } catch (Exception e) {
            log.error("导出库存预警商品失败", e);
            throw new RuntimeException("导出失败: " + e.getMessage());
        }
    }

    /**
     * 导出热销商品
     */
    @GetMapping("/products/hot")
    public void exportHotProducts(
            @RequestParam(defaultValue = "50") int limit,
            HttpServletResponse response) {
        try {
            log.info("开始导出热销商品，限制: {}", limit);
            List<Product> products = productService.getHotProducts(limit);
            ExcelExportUtil.exportProductsToExcel(products, response);
            log.info("导出热销商品成功，共{}条", products.size());
        } catch (Exception e) {
            log.error("导出热销商品失败", e);
            throw new RuntimeException("导出失败: " + e.getMessage());
        }
    }
}